package net.mysoft.budgetsoft.system.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import net.mysoft.budgetsoft.system.dao.GongNengDao;
import net.mysoft.budgetsoft.system.domain.TGongNeng;
import net.mysoft.budgetsoft.system.domain.comparator.GongNengComparator;
import net.mysoft.budgetsoft.system.pagemodel.DictionaryDetail;
import net.mysoft.budgetsoft.system.pagemodel.GongNeng;
import net.mysoft.budgetsoft.system.service.GongNengService;
import net.mysoft.framework.cache.Cache;
import net.mysoft.framework.cache.CacheManager;
import net.mysoft.framework.model.DataGrid;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * 
 * @author dagongPGP v1.0
 * @link <a href=http://www.dagongsoft.net>北京大公信软科技有限公司</a>
 * 
 * @version $Revision: 1.00 $Date: 2013-12-31 10:56:14
 */
@Service("gongNengService")
public class GongNengServiceImpl implements GongNengService {
	@Resource
	private GongNengDao gongNengDao;

	public GongNengDao getGongNengDao() {
		return this.gongNengDao;
	}

	public void setGongNengDao(GongNengDao gongNengDao) {
		this.gongNengDao = gongNengDao;
	}

	@Override
	public void add(GongNeng gongNeng) {
		TGongNeng t = new TGongNeng();
		BeanUtils.copyProperties(gongNeng, t);
		if (t.getBianHao() == null || "".equals(t.getBianHao())) {
			t.setBianHao(UUID.randomUUID().toString());
		}
		if (gongNeng.getShangJiBH() != null && !"".equals(gongNeng.getShangJiBH())) {
			TGongNeng p = (TGongNeng) gongNengDao.get(TGongNeng.class, gongNeng.getBianHao());
			t.setPtGongNeng(p);
		}
		gongNengDao.save(t);
	}

	@Override
	public void edit(GongNeng gongNeng) {
		TGongNeng t = new TGongNeng();
		BeanUtils.copyProperties(gongNeng, t);
		if (gongNeng.getShangJiBH() != null && !"".equals(gongNeng.getShangJiBH())) {
			TGongNeng p = (TGongNeng) gongNengDao.get(TGongNeng.class, gongNeng.getBianHao());
			t.setPtGongNeng(p);
		}
		gongNengDao.update(t);
	}

	@Override
	public void delete(GongNeng gongNeng) {
		for (String id : gongNeng.getIds().split(",")) {
			Object t = gongNengDao.get(TGongNeng.class, id);
			if (t != null) {
				gongNengDao.delete(t);
			}
		}
	}

	@Override
	public DataGrid datagrid(GongNeng gongNeng) {
		DataGrid dg = new DataGrid();
		String hql = "from TGongNeng t where 1=1 ";
		List params = new ArrayList();
		hql = addWhere(gongNeng, hql, params);
		hql = addOrder(gongNeng, hql);
		String totalHql = "select count(*) " + hql;
		List<TGongNeng> toList = gongNengDao.find(hql, params, gongNeng.getPage(), gongNeng.getRows());
		List<GongNeng> oList = new ArrayList<GongNeng>();
		oList = changeModel(toList);
		dg.setTotal(gongNengDao.count(totalHql, params));
		dg.setRows(oList);
		return dg;
	}

	private String addWhere(GongNeng gongNeng, String hql, List<Object> params) {
		if(gongNeng.getShangJiBH() != null && !"".equals(gongNeng.getShangJiBH())) {
			hql += " and t.ptGongNeng.bianHao = ?";
			params.add(gongNeng.getShangJiBH());
		}
		if(gongNeng.getLeiXing() != null && !"".equals(gongNeng.getLeiXing())) {
			hql += " and t.leiXing = ?";
			params.add(gongNeng.getLeiXing());
		}
		if(gongNeng.getMingCheng() != null && !"".equals(gongNeng.getMingCheng())) {
			hql += " and t.mingCheng like ?";
			params.add("%" + gongNeng.getMingCheng().trim() + "%");
		}
		if(gongNeng.getLuJing() != null && !"".equals(gongNeng.getLuJing())) {
			hql += " and t.luJing like ?";
			params.add("%" + gongNeng.getLuJing().trim() + "%");
		}
		return hql;
	}

	private String addOrder(GongNeng gongNeng, String hql) {
		if (gongNeng.getSort() != null) {
			hql += " order by " + gongNeng.getSort() + " " + gongNeng.getOrder();
		}
		return hql;
	}

	public List<GongNeng> changeModel(List<TGongNeng> tGongNeng) {
		Cache cac = CacheManager.getContent("ZhuangTai");
		List<DictionaryDetail> ddList = new ArrayList<DictionaryDetail>();
		if(cac!=null){
			ddList = (List<DictionaryDetail>) cac.getValue();
		}
		
		List<GongNeng> list = new ArrayList<GongNeng>();
		for (TGongNeng to : tGongNeng) {
			GongNeng o = new GongNeng();
			BeanUtils.copyProperties(to, o);
			for(DictionaryDetail dd:ddList){
				if(to.getZhuangTai()!=null){
					if(dd.getDetailID().equals(to.getZhuangTai().toString())){
						o.setZhuangTaiStr(dd.getDetailName());
						break;
					}
				}
			}
			if(to.getPtGongNeng() != null && to.getPtGongNeng().getBianHao() != null) {
				o.setShangJiBH(to.getPtGongNeng().getBianHao());
				o.setShangJiMC(to.getPtGongNeng().getMingCheng());
//				System.out.println("======*******o.getBianHao*******===========" + o.getBianHao());
//				System.out.println("======*******o.getIds*******===========" + o.getIds());
//				System.out.println("**********====o.getMingCheng=====******" + o.getMingCheng());
			}
			list.add(o);
		}
		return list;
	}

	@Override
	public List<GongNeng> find(GongNeng gongNeng) {
		String hql = "from TGongNeng t where 1=1";
		List params = new ArrayList();
		hql = addWhere(gongNeng, hql, params);
		List<TGongNeng> toList = gongNengDao.find(hql, params);
		Collections.sort(toList, new GongNengComparator());
		List<GongNeng> oList = new ArrayList<GongNeng>();
		oList = changeModel(toList);
		return oList;
	}
}
